package org.abratuhi.kvngroupware.jokemanager.model;

import java.util.Date;

import org.jdom.Element;

public class Joke {
	
	public final static String[] RATINGS = new String[]{"0", "1", "2", "3", "4", "5"};
	
	String id = new String();
	String text = new String();
	String rating = new String();
	
	String teamId = new String();
	String authorId = new String();
	
	public Joke(){
		
	}
	public Joke(String text, String author, String team, String rating){
		setId(text2id(author));
		setText(text);
		setAuthorId(author);
		setTeamId(team);
		setRating(rating);
	}
	
	public String getId(){
		return this.id;
	}
	public String getText(){
		return this.text;
	}
	public String getRating(){
		return this.rating;
	}
	public String getAuthorId(){
		return this.authorId;
	}
	public String getTeamId(){
		return this.teamId;
	}
	
	void setId(String id){
		this.id = id;
	}
	public void setText(String text){
		this.text = text;
	}
	public void setRating(String rating){
		this.rating = rating;
	}
	void setAuthorId(String author){
		this.authorId = author;
	}
	public void setTeamId(String team){
		this.teamId = team;
	}
	
	public static String text2id(String author){
		return (author + new Date().getTime());
	}
	
	
	public Element toElement(){
		Element eJoke = new Element("joke");
		
		eJoke.setText(getText());
		
		eJoke.setAttribute("id", getId());
		eJoke.setAttribute("authorId", getAuthorId());
		eJoke.setAttribute("teamId", getTeamId());
		eJoke.setAttribute("rating", getRating());
		
		return eJoke;
	}
	
	public static Joke fromElement(Element eJoke){
		Joke joke = new Joke();
		
		joke.setId(eJoke.getAttributeValue("id"));
		joke.setAuthorId(eJoke.getAttributeValue("authorId"));
		joke.setTeamId(eJoke.getAttributeValue("teamId"));
		joke.setRating(eJoke.getAttributeValue("rating"));
		joke.setText(eJoke.getText());
		
		return joke;
	}
	
	public Element toSmallElement(){
		Element ejoke = new Element("joke");
		ejoke.setAttribute("id", getId());
		return ejoke;
	}

}
